package com.example.epidemic_assistant.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.android.epidemic_assistant.gen.DaoSession;
import com.android.epidemic_assistant.gen.UserBeanDao;
import com.example.epidemic_assistant.App;
import com.example.epidemic_assistant.R;
import com.example.epidemic_assistant.bean.UserBean;
import com.example.epidemic_assistant.event.MqttGetDataEvent;
import com.example.epidemic_assistant.event.MqttShowToastEvent;
import com.example.epidemic_assistant.mqtt.MqttConnectManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 温度阈值
 */
public class TemperatureThresholdFragment extends Fragment {

    private final static String TAG = "TemperatureThresholdFragment";
    private final static String PUBLISH_TOPIC_POLICE = "temperature/download";//发布主题 - 报警温度

    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserBeanDao;
    private List<UserBean> mUserBeans;
    private EditText mEdtThreshold;
    private Button mBtnModifyThreshold;
    private String mTemperatureThreshold;
    private MqttConnectManager mMqttConnectManager;

    public static TemperatureThresholdFragment newInstance() {
        return new TemperatureThresholdFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_temperature_threshold, container, false);

        initView(view);
        initData();
        initListener();
        EventBus.getDefault().register(this);

        return view;
    }

    private void initListener() {
        mBtnModifyThreshold.setOnClickListener(view -> {
            String threshold = mEdtThreshold.getText().toString().trim();
            if (threshold.isEmpty()) {
                Toast.makeText(requireContext(),"温度阈值不能为空",Toast.LENGTH_SHORT).show();
            } else {
                mMqttConnectManager.publishMessage(PUBLISH_TOPIC_POLICE,
                        "{\"temperature\":"+threshold+"}");
            }
        });
    }

    private void initData() {
        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserBeanDao = mDaoSession.getUserBeanDao();
        mUserBeans = mUserBeanDao.loadAll();

        mMqttConnectManager = MqttConnectManager.getInstance();

        for (int i = 0; i < mUserBeans.size(); i++) {
            UserBean userBean = mUserBeans.get(i);
            mTemperatureThreshold = userBean.getTemperatureThreshold();
        }
        mEdtThreshold.setText(String.valueOf(mTemperatureThreshold));
    }

    private void initView(View view) {
        mEdtThreshold = view.findViewById(R.id.edt_threshold);
        mBtnModifyThreshold = view.findViewById(R.id.btn_modify_threshold);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttGetDataEvent(MqttGetDataEvent event) {
        Toast.makeText(requireContext(),"修改成功，温度阈值为:"+event.getMessage(),Toast.LENGTH_SHORT).show();
        for (int i = 0; i < mUserBeans.size(); i++) {
            UserBean userBean = mUserBeans.get(i);
            userBean.setTemperatureThreshold(event.getMessage());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMqttShowToastEvent(MqttShowToastEvent event) {
        Toast.makeText(requireContext(), event.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}